goog.provide('gaillard.MapFragment');

goog.require('goog.ui.Component');
goog.require('gaillard.Model');

/**
 *
 * @constructor
 * @extends {goog.ui.Component}
 */
gaillard.MapFragment = function (model) {
    goog.ui.Component.call(this);
    
    this.markers = [];
  
    this.setModel(model);
};

goog.inherits(gaillard.MapFragment, goog.ui.Component);


gaillard.MapFragment.prototype.createDom = function() {
	throw Error('Impossible to create MapFragment from code');
};

gaillard.MapFragment.prototype.decorateInternal = function(element) {
	gaillard.MapFragment.superClass_.decorateInternal.call(this, element);
	
	this.addrestaurant_ = goog.dom.getElement('header-add-restaurant');
    this.parameters_ = goog.dom.getElement('header-parameters');
    this.parametersmenu_ = goog.dom.getElement('header-parameters-menu');
    this.researchform_ = goog.dom.getElement('header-research-form');
    this.searchquery_ = goog.dom.getElement('header-search-query');
    
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(element, myOptions);
    
    if (navigator.geolocation) {    
        navigator.geolocation.getCurrentPosition(function(position) {                    
            var currentPosition = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
            map.setCenter(currentPosition);
        }, function() {
        
        });
    }
    
    this.map = map;
};

gaillard.MapFragment.prototype.enterDocument = function() {
	gaillard.MapFragment.superClass_.enterDocument.call(this);
    var eh = this.getHandler(),
        model = this.getModel();    
    
    eh.listen(model, gaillard.Model.Datatype.ADDRESS, this.addressChanged_);    
    eh.listen(model, gaillard.Model.Datatype.RESTAURANT_LIST, this.restaurantListChanged_);
    eh.listen(model, gaillard.Model.Datatype.FOCUSED_RESTAURANT, this.focusedRestaurantChanged_);
    eh.listen(model, gaillard.Model.Datatype.SELECTED_RESTAURANT, this.selectedRestaurantChanged_);
};

gaillard.MapFragment.prototype.addressChanged_ = function() {
    var address = this.getModel().get(gaillard.Model.Datatype.ADDRESS);
    this.map.fitBounds(address.geometry.bounds);
};

gaillard.MapFragment.prototype.restaurantListChanged_ = function() {
    var list = this.getModel().get(gaillard.Model.Datatype.RESTAURANT_LIST);
    this.clearRestaurantList();    
    for (var i = 0; i < list.length; i+=1) {
        this.addRestaurant(list[i]);                
    }
};

gaillard.MapFragment.prototype.clearRestaurantList = function() {
    var markers = this.markers;
    for (var i = 0; i < markers.length; i+=1) {
        markers[i].setMap(null);
    }
    this.markers = [];
};

gaillard.MapFragment.prototype.addRestaurant = function(restaurant) {
        
    var marker = new google.maps.Marker({
      map:this.map,
      animation: google.maps.Animation.DROP,
      position: new google.maps.LatLng(restaurant.lat, restaurant.lon)
    });
    
    this.markers.push(marker);
};

gaillard.MapFragment.prototype.selectedRestaurantChanged_ = function () {
    var position = this.getModel().get(gaillard.Model.Datatype.SELECTED_RESTAURANT);
    if (position > -1) {
        this.map.panTo(this.markers[position].getPosition());
    }
};

gaillard.MapFragment.prototype.focusedRestaurantChanged_ = function () {
    var position = this.getModel().get(gaillard.Model.Datatype.FOCUSED_RESTAURANT);
    var markers = this.markers;
    for (var i = 0; i < markers.length; i+=1) {
        if (i == position) {
            markers[i].setAnimation(google.maps.Animation.BOUNCE);
        } else {
            markers[i].setAnimation(null);
        }
    }
};


